package com.erp.erp_project.controller;

import com.erp.erp_project.core.annotation.Permission;
import com.erp.erp_project.core.base.AjaxResult;
import com.erp.erp_project.core.exception.CustomException;
import com.erp.erp_project.model.Power;
import com.erp.erp_project.model.Role;
import com.erp.erp_project.model.Submenu;
import com.erp.erp_project.service.PowerService;
import com.erp.erp_project.service.RoleService;
import com.erp.erp_project.service.SubmenuService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author yh
 */
@Controller
@AllArgsConstructor
@RequestMapping("/role")
public class RoleController {
    private RoleService roleService;
    private SubmenuService submenuService;
    private PowerService powerService;

    @GetMapping("/list")
    public String list(){
        return "role/list";
    }

    @PostMapping("/list")
    @ResponseBody
    public AjaxResult<PageInfo<Role>> listrole(Role role, Integer page, Integer limit, String rolefield, String condition){
        PageHelper.startPage(page,limit);
        List<Role> roles=roleService.relist(role,rolefield,condition);
        PageInfo<Role> pageInfo = new PageInfo<Role>(roles);
        return new AjaxResult<PageInfo<Role>>(pageInfo);
    }

    @GetMapping("/add")
    @Permission("角色添加")
    public String add(Model model){
        List<Submenu> submenus=submenuService.list(new Submenu());
        model.addAttribute("submenus",submenus);
        List<Power> powers=powerService.list(new Power());
        model.addAttribute("powers",powers);
        return "role/add";
    }

    @PostMapping("/add")
    @Permission("角色添加")
    @ResponseBody
    public AjaxResult addrole(String ids,Role role) throws CustomException {
        ids=ids.replace("[","");
        ids=ids.replace("]","");
        ids=ids.replace("\"","");
        roleService.addrole(ids,role);
        return new AjaxResult("添加成功");
    }

    @PostMapping("/delete")
    @ResponseBody
    @Permission("角色删除")
    public AjaxResult delete(String roleIds) throws CustomException {
        String[] rid=roleIds.split(",");
        List ids= Arrays.asList(rid);
        roleService.delete(ids);
        return new AjaxResult("删除成功");
    }

    @GetMapping("/update")
    @Permission("角色修改")
    public String update(Integer roleId, Model model){
        Role role=roleService.query(roleId);
        List<Submenu> submenus=submenuService.list(new Submenu());
        model.addAttribute("submenus",submenus);
        String strs=powerService.query(roleId);

        List<Power> powers=null;
        List<Power> powers1=null;
        if(strs!=null&&!strs.trim().equals("")){
            //角色的权限
            powers=powerService.selects(strs);
            List<String> list=new ArrayList<String>();
            for (Power power:powers
            ) {
                list.add(power.getName());
            }

            powers1=powerService.list(new Power());
            //角色没有的权限
            Iterator<Power> it=powers1.iterator();
            while (it.hasNext()){
                if(list.contains(it.next().getName())){
                    it.remove();
                }
            }

        }else {
            powers1=powerService.list(new Power());
        }


        model.addAttribute("powers",powers);
        model.addAttribute("powers1",powers1);
        model.addAttribute("role",role);
        return "role/update";
    }

    @PostMapping("/update")
    @ResponseBody
    @Permission("角色修改")
    public AjaxResult updateRole(String ids, Role role, HttpServletRequest request) throws CustomException {
        roleService.updatarole(role);
        ids=ids.replace("[","");
        ids=ids.replace("]","");
        ids=ids.replace("\"","");
        roleService.updatarp(ids,role.getRoleId());
        return new AjaxResult("保存成功");
    }

    @GetMapping("/power")
    @Permission("角色修改")
    public String power(Integer roleId, Model model){
        Role role=roleService.query(roleId);
        List<Submenu> submenus=submenuService.list(new Submenu());
        model.addAttribute("submenus",submenus);
        String strs=powerService.query(roleId);
        List<Power> powers=null;
        List<Power> powers1=null;
        if(strs!=null&&!strs.trim().equals("")){
            //角色的权限
            powers=powerService.selects(strs);
            List<String> list=new ArrayList<String>();
            for (Power power:powers
            ) {
                list.add(power.getName());
            }

            powers1=powerService.list(new Power());
            //角色没有的权限
            Iterator<Power> it=powers1.iterator();
            while (it.hasNext()){
                if(list.contains(it.next().getName())){
                    it.remove();
                }
            }

        }else {
            powers1=powerService.list(new Power());
        }
        model.addAttribute("powers",powers);
        model.addAttribute("powers1",powers1);
        model.addAttribute("role",role);
        return "role/power";
    }

    @PostMapping("/power")
    @ResponseBody
    @Permission("角色修改")
    public AjaxResult powerRole(String ids, Role role, HttpServletRequest request) throws CustomException {
        ids=ids.replace("[","");
        ids=ids.replace("]","");
        ids=ids.replace("\"","");
        roleService.updatarp(ids,role.getRoleId());
        return new AjaxResult("保存成功");
    }


}
